<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/blog_list.css">
</head>

<body>
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- spacer 不现实任何内容, 只是占位, 把后面 a 标签撑到右侧去 -->
        <div class="spacer"></div>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="logout">注销</a>
        
    </div>

    
    <!-- 这个 div 表示页面的主区域(版心) -->
    <div class="container">
        <!-- 左侧的用户信息 -->
        <div class="container-left">
            <!-- 用这个元素表示用户的信息 -->
            <div class="card">
                <!-- 用户的头像 -->
                <img src="img/狗头.png" alt="">
                <!-- 用户的名字 -->
                <h3></h3>
                <a href="#">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>

                
            </div>
        </div>
        <!-- 右侧内容区域 -->
        <div class="container-right">
            <!-- 每个 .blog 用来表示一篇博客 -->
            <!-- <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2022-11-06 12:00:00</div>
                <div class="desc">
                    从今天起, 我要认真敲代码. Lorem ipsum dolor sit amet consectetur adipisicing elit. Id veritatis repudiandae, at quas temporibus iure minima aliquid facilis! Odit unde veniam, veritatis qui illum tenetur in dolores sint asperiores aliquam?
                </div>
                <a href="blog_detail.html">查看全文 &gt;&gt; </a>
            </div> -->
        </div>
    </div>
    <!-- 强制登录逻辑 -->
    <script src="js/app.js"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script>
        //发送ajax请求从服务器获取博客列表页数据
        function getBlogs() {
            $.ajax({
                type: 'get',
                url: 'blog',
                success: function(body) {
                    let container = document.querySelector('.container-right');
                    for(let blog of body) {
                        //构造blogDiv
                        let blogDiv = document.createElement('div');
                        blogDiv.className = 'blog';

                        //构造标题
                        let titleDiv = document.createElement('div');
                        titleDiv.className = 'title';
                        titleDiv.innerHTML = blog.title;

                        //构造时间
                        let dateDiv = document.createElement('div');
                        dateDiv.className = 'date';
                        dateDiv.innerHTML = blog.postTime;

                        //构造正文
                        let contentDiv = document.createElement('div');
                        contentDiv.className = 'desc';
                        contentDiv.innerHTML = blog.content;

                        //查看全文                       
                        let a = document.createElement('a');
                        //这里需要加上一个query string, 通过blogId来找到相应的博客
                        a.href = 'blog_detail.html?blogId=' + blog.blogId;
                        a.innerHTML = '查看全文 &gt;&gt;';
                        
                        //拼装
                        container.appendChild(blogDiv);
                        blogDiv.appendChild(titleDiv);
                        blogDiv.appendChild(dateDiv);
                        blogDiv.appendChild(contentDiv);
                        blogDiv.appendChild(a);

                    }
                }
            });
        }
        getBlogs();
    </script>

</body>

</html>